<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_onlinestats_start() {
	global $adb;
	
	$session_id = $_SESSION['suser']->sid;
	$users_id = $_SESSION['suser']->userid;
	$users_name = $_SESSION['suser']->name;
	
    $ip_address = getenv('REMOTE_ADDR');
    $last_page_url = getenv('REQUEST_URI');
	
    $current_time = time();
    $xx_mins_ago = ($current_time - 900);
	
	//remove entries that have expired
	$adb->query("delete from pp_onlinestats where onlinestats_time_lastclick < '" . $xx_mins_ago . "'");
	
	//
	$stored_user_result = $adb->query("select count(*) as count from pp_onlinestats where onlinestats_session = '" . func_db_input($session_id) . "'");
	$stored_user = $adb->fetch_array($stored_user_result);
	if( $stored_user['count'] > 0 ) {
		$adb->query("update pp_onlinestats set users_id = '" . $users_id . "', onlinestats_name = '" . $users_name . "', onlinestats_ipaddress = '" . $ip_address . "', onlinestats_time_lastclick = '" . $current_time . "', onlinestats_last_pageurl = '" . $last_page_url . "' where onlinestats_session = '" . $session_id . "'");
	} else {
		$adb->query("insert into pp_onlinestats (users_id, onlinestats_session, onlinestats_ipaddress, onlinestats_name, onlinestats_fullname, onlinestats_time_entry, onlinestats_time_lastclick, onlinestats_last_pageurl) values ('" . $users_id . "', '" . $session_id . "', '" . $ip_address . "', '" . $users_name . "', '" . $users_fullname . "', '" . $current_time . "', '" . $current_time . "', '" . $last_page_url . "')");

	}
}
//
?>